import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'mpaas_platform_interface.dart';

/// An implementation of [MpaasPlatform] that uses method channels.
class MethodChannelMpaas extends MpaasPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('mpaas');

  /// The event channel used to receive events from the native platform.
  @visibleForTesting
  final eventChannel = const EventChannel('mpaas_events');

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<String?> pushGetToken() async {
    final version = await methodChannel.invokeMethod<String>('getToken');
    return version;
  }

  @override
  Future<bool> pushBindUserId(String userId) async {
    final result = await methodChannel.invokeMethod<bool>('pushBindUserId', {'userId': userId});
    if (result == null) {
      throw PlatformException(
        code: 'ERROR',
        message: 'Failed to bind user ID',
      );
    }
    return result;
  }

  @override
  Future<bool> pushUnbindUserId(String userId) async {
    final result = await methodChannel.invokeMethod<bool>('pushUnbindUserId', {'userId': userId});
    if (result == null) {
      throw PlatformException(
        code: 'ERROR',
        message: 'Failed to unbind user ID',
      );
    }
    return result;
  }

  @override
  Future<bool> pushBindUserIdWithPhone(String userId, String phoneNumber) async {
    final result = await methodChannel.invokeMethod<bool>('pushBindUserIdWithPhone', {
      'userId': userId,
      'phoneNumber': phoneNumber,
    });
    if (result == null) {
      throw PlatformException(
        code: 'ERROR',
        message: 'Failed to bind user ID with phone number',
      );
    }
    return result;
  }
}
